package com.hzpd.hainan.module.news.view;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzpd.hainan.R;
import com.hzpd.hainan.custorm.TopPicViewPager;
import com.hzpd.hainan.model.news.NewsFlashBean;
import com.hzpd.hainan.module.news.adapter.TopviewpagerAdapter;
import com.hzpd.hainan.module.news.fragment.NewsItemFragment;
import com.hzpd.hainan.utils.LogUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 猪猪侠 on 2017/7/25.
 */

public class TopViewPagerView  {
    TopPicViewPager testPager;//viewpager
    TextView viewpageTxtId;//
    TextView viewpageTxtMolecular;
    TextView viewpageTxtDenominator;
    private Handler viewHandler = new Handler();
    private MyRun switchTask;//切换线程
    private boolean isContinue = true;//是否可以自动切换
    private TopviewpagerAdapter topAdapter;
    private int interval = 4;//播放间隔时间
    private View rootView;
    private Activity mActivity;


    public static TopViewPagerView instance(Activity activity){

        return new TopViewPagerView(activity);
    }

    public TopViewPagerView(Activity activity){
        //
        this.mActivity = activity;
        initView(activity);
    }

    public View getView(){
        return rootView;
    }

    /**
     * @param context
     */
    private void initView(Activity context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        rootView = layoutInflater.inflate(R.layout.view_news_topviewpager,null);
        testPager= rootView.findViewById(R.id.test_pager);
        viewpageTxtId= rootView.findViewById(R.id.viewpage_txt_id);
        viewpageTxtMolecular= rootView.findViewById(R.id.viewpage_txt_Molecular);
        viewpageTxtDenominator= rootView.findViewById(R.id.viewpage_txt_Denominator);
        topAdapter = new TopviewpagerAdapter(context);
        testPager.setAdapter(topAdapter);
        viewHandler.removeCallbacksAndMessages(null);
        switchTask = new MyRun();
        testPager.setCurrentItem(0);
        testPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == topAdapter.getCount() - 1) {
                    testPager.setCurrentItem(1, false);
                    return;
                } else if (0 == position) {
                    testPager.setCurrentItem(topAdapter.getCount() - 2, false);
                }
                viewpageTxtId.setText(topAdapter.getBean(position).getTitle());//设置小标题
                viewpageTxtMolecular.setText(topAdapter.getPosition(position) + "");
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    public void reFreshFlashData(List<NewsFlashBean> mViewPagelist){

        viewpageTxtId.setText(mViewPagelist.get(0).getTitle());

        topAdapter.setData(mViewPagelist);

        viewpageTxtMolecular.setText("1");
        if (mViewPagelist.size() > 1) {
            viewpageTxtDenominator.setText("/" + (mViewPagelist.size() - 2));
            testPager.setCurrentItem(1, false);
        } else {
            viewpageTxtDenominator.setText("/" + mViewPagelist.size());
        }
        if (topAdapter.getListSize() > 1) {
            if (switchTask.isStart){
                viewHandler.removeCallbacks(switchTask);
            }
            switchTask.run();
        }
    }
    /**
     * 轮播定时滚动
     */
    /**
     * 轮播定时滚动
     */
    class MyRun implements Runnable {
        private boolean isStart = false;

        public void run() {
            if (isContinue && isStart) {

                int current = testPager.getCurrentItem();
                int total = testPager.getAdapter().getCount();
                if (total == current + 1) {
                    testPager.setCurrentItem(0, false);
                } else {
                    testPager.setCurrentItem(current + 1, true);
                }
            } else {
                isStart = true;
            }

            viewHandler.postDelayed(switchTask, interval * 1000);
        }
    }

}
